package day14.homework02.test03;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author Xiao
 * @date 2021/8/7
 */
public class Foodie implements Runnable {
    private Desk desk;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                if (desk.getCount() == 0) {
                    break;
                }
                if (desk.isFlag()) {
                    System.out.println("吃货吃了1个汉堡包。。。");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    desk.setCount(desk.getCount() - 1);
                    desk.setFlag(false);
                    desk.getLock().notifyAll();
                } else {
                    try {
                        desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
